package cn.inbot.shopadmin.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.inbot.shopadmin.R;

/**
 * Created by shisenkun on 2021/5/10.
 */
public class RefreshAnimView extends View {

    private float scaleFloat1 = 1f;
    private float scaleFloat2 = 1f;
    private float degrees = 0f;
    private Paint mPaint = new Paint();
    private Map<ValueAnimator, ValueAnimator.AnimatorUpdateListener> mUpdateListeners =
            new HashMap<>();
    private List<ValueAnimator> mAnimators = new ArrayList<>();

    public RefreshAnimView(Context context) {
        this(context, null);
    }

    public RefreshAnimView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mPaint.setColor(ContextCompat.getColor(context, R.color.white));
        mPaint.setAntiAlias(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float circleSpacing = 12f;
        float x = getWidth() / 2f;
        float y = getHeight() / 2f;

        canvas.save();
        canvas.translate(x, y);
        canvas.scale(scaleFloat1, scaleFloat1);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(0f, 0f, x / 2.5f, mPaint);

        canvas.restore();

        canvas.translate(x, y);
        canvas.scale(scaleFloat2, scaleFloat2);
        canvas.rotate(degrees);

        mPaint.setStrokeWidth(3f);
        mPaint.setStyle(Paint.Style.STROKE);

        float[] startAngles = new float[]{225.0F, 45.0F};
        int i = 0;

        for (int j = 1; i <= j; ++i) {
            RectF rectF = new RectF(-x + circleSpacing, -y + circleSpacing, x - circleSpacing, y - circleSpacing);
            canvas.drawArc(rectF, startAngles[i], 90.0F, false, this.mPaint);
        }
    }

    public void show() {
        setVisibility(View.VISIBLE);
        scaleFloat1 = 0.75f;
        scaleFloat2 = 0.75f;
        degrees = 0f;
        invalidate();
    }

    public void startAnim() {
        mAnimators = createAnimators();

        for (ValueAnimator animator : mAnimators) {
            ValueAnimator.AnimatorUpdateListener updateListener = mUpdateListeners.get(animator);
            if (updateListener != null) {
                animator.addUpdateListener(updateListener);
            }
            animator.start();
        }
    }

    public void hide() {

        for (ValueAnimator animator : mAnimators) {
            ValueAnimator.AnimatorUpdateListener updateListener = mUpdateListeners.get(animator);
            if (updateListener != null) {
                animator.removeAllUpdateListeners();
                animator.end();
            }
            animator.start();
        }
        mAnimators.clear();
        mUpdateListeners.clear();
        setVisibility(GONE);
    }

    private List<ValueAnimator> createAnimators() {
        ValueAnimator scaleAnim = ValueAnimator.ofFloat(0.75f, 1f, 0.3f, 1f);
        scaleAnim.setDuration(1500L);
        scaleAnim.setRepeatCount(-1);
        addUpdateListener(scaleAnim, animation -> {
            scaleFloat1 = (float) animation.getAnimatedValue();
            postInvalidate();
        });
        ValueAnimator scaleAnim2 = ValueAnimator.ofFloat(0.75f, 1f, 0.6f, 1f);
        scaleAnim2.setDuration(1500L);
        scaleAnim2.setRepeatCount(-1);
        addUpdateListener(scaleAnim2,animation -> {
            scaleFloat2 = (float) animation.getAnimatedValue();
            postInvalidate();
        });
        ValueAnimator rotateAnim = ValueAnimator.ofFloat(0f, 180f, 360f);
        rotateAnim.setDuration(1500L);
        rotateAnim.setRepeatCount(-1);
        addUpdateListener(rotateAnim, animation -> {
            degrees = (float) animation.getAnimatedValue();
            postInvalidate();
        });
        List<ValueAnimator> animators = new ArrayList<> ();
        animators.add(scaleAnim);
        animators.add(scaleAnim2);
        animators.add(rotateAnim);
        return animators;
    }

    private void addUpdateListener(ValueAnimator animator,
                                         ValueAnimator.AnimatorUpdateListener updateListener) {
        mUpdateListeners.put(animator, updateListener);
    }


}

